<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .container {
            width: 1226px;
            margin: 50px auto;
            display: flex;
            justify-content: space-between;
        }

        .container .magnifier-wrapper {
            width: 400px;
            height: 600px;
            /* background-color: orange; */
            position: relative;
        }

        .container .magnifier-wrapper .magnifier {
            width: 400px;
            height: 400px;
            background-color: rgba(0, 0, 0, 0.5);
            position: relative;
        }

        .container .magnifier-wrapper .magnifier .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }

        .container .magnifier-wrapper .magnifier img {
            width: 100%;
            height: 100%;
        }

        .container .magnifier-wrapper .magnifier-list {
            width: 400px;
            height: 100px;
            background-color: rgba(0, 0, 0, 0.5);
            margin-top: 20px;
        }

        .container .magnifier-wrapper .magnifier-preview {
            width: 500px;
            height: 500px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            /* -webkit-calc(100% + 20px) 计算函数 =>在css中使用计算操作 */
            /* left: -webkit-calc(100% + 20px); */
            left: 420px;
            top: 0;
            overflow: hidden;
            display: none;
        }

        .magnifier-preview img {
            width: 1000px;
            height: 1000px;
            position: relative;
            left: 0;
            top: 0;
            /* z-index: -1; */
        }

        .container .item-info {
            flex: 1;
            margin-left: 20px;
            height: 600px;
            /* background-color: pink; */
        }
    </style>
</head>

<body>
    <!-- .container>.magnifier-wrapper>.magnifier+.magnifier-list+.magnifier-preview -->
    <div class="container">
        <!-- position:relative -->
        <div class="magnifier-wrapper">

            <!-- magnifier.offsetLeft 相对于父元素的 -->
            <div class="magnifier">
                <img class="smallImg" src="../images/girlsmall1.jpg" alt="">
                <div class="shadow"></div>
            </div>
            <div class="magnifier-list"></div>

            <!-- position:absolute -->
            <div class="magnifier-preview">
                <img class="bigImg" src="../images/girlbig1.jpg" alt="">
            </div>
        </div>
        <div class="item-info"></div>
    </div>


</body>
<script>

    /* 
    放大镜实现
    1. shadow遮住的区域,就是右边大盒子magnifier-preview中显示的区域  => 呈比例关系放大(布局相关)

        shadow(200)                smallImg(400)            x
    ——————————————————      ==    ——————————————   ==     —————— 
     magnifierPreview(500)          bigImg(1000)            X(=> -2.5x)
    
    2. 左边移动的是shadow，右边移动的是图片(图片 => 布局相关)

    3. shadow移动的距离和bigImg移动的距离, 也是等比例且反向的
    
    
    */

    /* 
       问题: 添加移入移出之后,1. shadow可以超出临界值  右边图片不动了
 
       shadow可以超出临界值 =>  maxLeft 有问题 => 测试发现,最终是shadow.clientWidth =0 => 分析: shadow设置了display:none =>  导致=> shadow.clientWidth = 0
    
       右边图片不动了 => 图片的值依赖于 scale * x ,shadow可以动,x没问题 => scale有问题 (0) => magnifierPreview设置了display:none => bigImg也隐藏了 => bigImg.clientWidth =  0 
       
       问题总结: shadow magnifierPreview 设置隐藏(display:none)导致的  => shadow.clientWidth bigImg.clientWidth 都是  0 


        解决方案: 

    */



    var magnifierWrapper = document.getElementsByClassName("magnifier-wrapper")[0];
    var magnifier = document.getElementsByClassName("magnifier")[0];
    var magnifierPreview = document.getElementsByClassName("magnifier-preview")[0];
    var shadow = document.getElementsByClassName("shadow")[0];
    var smallImg = document.getElementsByClassName("smallImg")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];

    var maxLeft = magnifier.clientWidth - shadow.clientWidth;
    var maxTop = magnifier.clientHeight - shadow.clientHeight;
    console.log(magnifier.clientWidth, shadow.clientWidth);

    var scale = bigImg.clientWidth / smallImg.clientWidth; // 2.5
    console.log("scale", scale);

    magnifier.onmouseenter = function () {
        shadow.style.display = "block";
        magnifierPreview.style.display = "block";
    }

    magnifier.onmousemove = function (e) {
        var e = e || window.event;

        var x = e.pageX - magnifierWrapper.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - magnifierWrapper.offsetTop - shadow.clientHeight / 2;

        if (x < 0) x = 0;
        if (x > maxLeft) x = maxLeft;

        if (y < 0) y = 0;
        if (y > maxTop) y = maxTop;

        // shadow移动
        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        // bigImg移动
        bigImg.style.left = -scale * x + "px";
        bigImg.style.top = -scale * y + "px";
    }

    magnifier.onmouseleave = function () {
        shadow.style.display = "none";
        magnifierPreview.style.display = "none";
    }



</script>

</html>